package com.pe.pgn.clubpgn.webapp.controller.meta;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.domain.ClpdMeta;
import com.pe.pgn.clubpgn.service.MetaService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;

/**
 * @author Frank
 * */

@Controller
@RequestMapping("/programa/meta/**")
public class MetaController extends BaseFormController{

	@Autowired
	private MetaService metaService;
	
	@RequestMapping(value = "/metaform.html")
	public String verMeta(@RequestParam(value = "id", required = false) Long idPrograma, 
			HttpServletRequest request,Model modelMap) {

		try {

			List<Map<String,Object>> metasPorPrograma = 
			metaService.metasPorProgramas(idPrograma);
			
			ClpdMeta clpdMeta = new ClpdMeta();
			clpdMeta.setClpbPrograma(metaService.findPrograma(idPrograma));
			
			modelMap.addAttribute("meta"	   , clpdMeta);
	    	modelMap.addAttribute("listametas" , metasPorPrograma);
			return "/programa/meta/metaform";
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "/programa/meta/metaform";
		}
	}
	
	@RequestMapping(value = "/eliminarmeta.html")
	public String eliminarPrograma(
	@RequestParam(value = "id", required = false) Long id,
	@RequestParam(value = "idPrograma", required = false) Long idPrograma,
			HttpServletRequest request,Model modelMap) {

		try {

			metaService.eliminarMeta(id);
			saveMessage(request, getText("metaform.deleted", request.getLocale()));
			
			List<Map<String,Object>> metasPorPrograma = 
			metaService.metasPorProgramas(idPrograma);
			
			ClpdMeta clpdMeta = new ClpdMeta();
			clpdMeta.setClpbPrograma(metaService.findPrograma(new Long(idPrograma)));
			
			modelMap.addAttribute("meta"	   , clpdMeta);
	    	modelMap.addAttribute("listametas" , metasPorPrograma);
			return "/programa/meta/metaform";
			
		} catch (Exception e) {
			
			saveError(request, getText("errors.general", request.getLocale()));
			return "/programa/meta/metaform";
		}
	}
	
	@RequestMapping(value = "/metaform.html", method = RequestMethod.POST)
	public String guardarMeta(@ModelAttribute("meta") ClpdMeta meta,
	BindingResult result, SessionStatus status, HttpServletRequest request,Model modelMap) {
		
		try {
			
			List<Map<String,Object>> metasPorPrograma = 
			metaService.metasPorProgramas(new Long(meta.getClpbPrograma().getId()));
			
			ClpdMeta clpdMeta = new ClpdMeta();
			clpdMeta.setClpbPrograma(metaService.findPrograma(meta.getClpbPrograma().getId()));
			
			modelMap.addAttribute("meta"	   , clpdMeta);
	    	modelMap.addAttribute("listametas" , metasPorPrograma);
			
			if (result.hasErrors()) {
				return "/programa/meta/metaform";
			}

			metaService.saveOrUpdateMeta(meta);
			saveMessage(request, getText("metaform.added", request.getLocale()));
			
			metasPorPrograma = 
			metaService.metasPorProgramas(new Long(meta.getClpbPrograma().getId()));
			modelMap.addAttribute("listametas" , metasPorPrograma);
			
			return "/programa/meta/metaform";
			
		} catch (Exception e) {
			saveError(request, getText("errors.general", request.getLocale()));
			return "/programa/meta/metaform";
		}
	}
}
